package com.xing.photo.funcation;

import com.xing.photo.util.StaticUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/3/2
 * Time: 16:33
 */
public class Preview {
    static String uids = "1653195901,1914186102,1147018820,5133956570,2001169861,1869746690,1764387772,5513205378,2094680534,1784154731,5251079420,3631130204,3107047217,1651913845,1237332783,2310196137,3893571172,3663114442,5352652216,5175164046,1769075055,2147891682,1765632395,1903828135,2163537072,2946213722,2943601971,2807235412,1759192487,5514452503,2007003503,5305409476,5332409442,2029177923,2192655902,2991571237,5248731982,2817400414,2369513884,5495932814,3286790770,1838717532,1777591071,5395629748,1785831892,2656912107,1758317661,5489949483,5288846520,3845517368,3199232173,5075495414,5283835193,2659354961,1986832233,5237499621,1751185610,1618081915,1854213013,1833936672,1805319992,2065370947,2212970554,5237359659,2858285415,5473858365,5230449233,1870487421,3203066140,1631172140,1689212192,5130612366,2904133005,3520432890,5063793134,1789505151,1667575473,1800326274,5373870031,2317382462,5498732258,3121627857,2925218875,2339477240,2245399551,2927805375,2696378991,2106957070,1783903497,1773338797,5421153904,3810075288,1915745023,1641129767,1806051377,2391374357,5120518135,1903755563,3799589875,5465705232,5126458019,3866984432,1729754325,3483719234,5044576689,1987977925,1885588824,5307988724,3866519978,5252769783,1734694237,1787906883,2385710061,2589379191,1659490917,3197244522,2874658450,5293403614,1851280897,3266094581,2101374042,3195736930,1808857844,1763276065,3301957245,1028013932,3226194611,5331848875,2501785757,2397749437,2930874374,2646972904,1775949981,5158415673,2627177225,5430655635,5358675372,1827620680,5348212102,2212415384,2755036293,2163818223,2450406524,1742693175,1794308181,2527924824,2275032353,3847683028,1821414794,5346520259,3782403782,1776080327,5374908789,3192829182,1916401801,1662333790,5036188074,1927816863,1746653972,3057156031,1639739404,3941263912,5381279826,5396359618,1639828700,5186869517,3267647333,3171619851,1758772173,1891056002,5187169946,5246886330,2234149310,3980460995,3807811850,3948028803,1770846257,2300460761,3571972452,5389762754,1605344042,1749063237,1115380781,1559410915,1306275014,1841923530,1785574792,5332209398,2192446713,2056482544,1712831753,1783336362,5305473125,1760365341,3883998834,5294118660,5385006426,2739301765,1892052062,1839807553,1703697272,3986265903";

    public static void main(String[] args) throws URISyntaxException, IOException {
        Pattern pattern = Pattern.compile("CONFIG\\['onick'\\]='(.*?)';");
        String[] suid = uids.split(",");
        for (String uid : suid) {
            URI uri = new URI("http://photo.weibo.com/albums/get_all?uid=" + uid + "&page=1&count=5");
            String content = getContent(uri);
            ObjectMapper mapper = new ObjectMapper();
            JsonNode resultNode = mapper.readTree(content);
            JsonNode list = resultNode.get("data").get("album_list");
            if (list.get(1).get("caption").asText().equals("微博配图")) {
                if (list.get(1).get("count").get("photos").asInt() < 10000) {
                    URI weibo = new URI("http://weibo.com/u/"+uid);
                    String profile = getContent(weibo);
                    Matcher matcher = pattern.matcher(profile);
                    if(matcher.find()){
//                        System.out.println("album_id: " + list.get(1).get("album_id").asLong() + " photos: " + list.get(1).get("count").get("photos").asInt() + " uid: " + uid + " name: " + matcher.group(1));
                        System.out.println("INSERT INTO USER (Fuid,Falbum_id,Fname) VALUES ("+uid+","+list.get(1).get("album_id").asLong()+",'"+matcher.group(1)+"');" + " total: " + list.get(1).get("count").get("photos").asInt());
                    }
                }
            }
        }
    }

    private static String getContent(URI uri) {
        String result = null;
        try {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Cookie", StaticUtil.cookies);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity re = response.getEntity();
            result = EntityUtils.toString(re, "gbk");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
